import { reactive, ref } from "vue"
import type { ICustomHttpResponse, ITabsList } from "@/simba/types";
import type { siTabsInstance } from "@/simba/types/component.d";
import type { ILocationBreed } from "@/simba/types/plant.d";
import LocationBreedApi from "@/sheep/api/huadada/locationbreed";
import sheep from '@/sheep';

export const useLocationBreed =()=>{
	const listLocBreedType = [{name:'位置'}, {name: '品种'}]//固定信息
	
	const rules = {
	  name: {
	    rules: [
	      {
	        required: true,
	        errorMessage: '请输入名称',
	      },
	    ],
	  },
	};
	
	function onNavLocBreedEditHandler(locBreedIndex: number, id?:number){
		// toLocBreedEdit(listLocBreedType[locBreedIndex].name)
		let urllb = '/pages/plant/locBreed/LocBreedEdit?type=' + listLocBreedType[locBreedIndex].name
		if(id)
			urllb = urllb + '&id=' + id
			
		sheep.$router.go(urllb)
	}
	
	async function getLocationBreed(id: number){
		const { code, data } = await LocationBreedApi.getLocationBreed(id)  as ICustomHttpResponse
		if(code !== 0){
			return
		}
		// formData.value = data
		return data
	}

	async function getLocationBreedList(uid: number){
		const { code, data } = await LocationBreedApi.getLocationBreedList(uid)  as ICustomHttpResponse
		if(code !== 0){
			return
		}
		// listLocBreeds.value = data
		return data
	}

	async function addLocationBreed(formData: ILocationBreed){
		//当前用户添加自己的位置、品种
		const { code } = formData.id as number > 0 
			? await LocationBreedApi.updateLocationBreed(formData)  as ICustomHttpResponse
			: await LocationBreedApi.addLocationBreed(formData)  as ICustomHttpResponse
		if(code === 0){
			uni.showToast({
				icon: 'success',
				title: '新增成功',
				duration: 1000
			})
			setTimeout(()=>{
				uni.hideToast()
				sheep.$router.back()
			}, 1000)
		}
	}
	
	async function deleteLocationBreed(formData: ILocationBreed){
		//当前用户添加自己的位置、品种
		uni.showModal({
			title: '提示',
			content: '确定要删除' + formData.type + '吗？',
			success: async function (res) {
				if(!res.confirm){
					return
				}
				const { code } = await LocationBreedApi.deleteLocationBreed(formData.id)  as ICustomHttpResponse
				
				if(code === 0){
					uni.showToast({
						icon: 'success',
						title: '删除成功',
						duration: 1000
					})
					setTimeout(()=>{
						uni.hideToast()
						sheep.$router.back()
					}, 1000)
				}
			}
		})
	}
	
	return {
		listLocBreedType, rules, onNavLocBreedEditHandler, 
		getLocationBreed, getLocationBreedList, addLocationBreed, deleteLocationBreed
	}
}

const defaultTabItem = { id: 0, name: '全部' }

//植物页面中位置信息的选择
export const useLocationTabs = () => {
	
	let locationTabs = reactive<ITabsList>({
		list: [	defaultTabItem ],
		choosedTab: defaultTabItem //默认为选择，与list中的
	})
	const locationTabRef = ref<siTabsInstance>();//
	function onGetLocationDataHandler(){
		locationTabs.list.push(
			{id: 1, name: '户外'},
			{id: 2, name: '阳台'})
	}
	
	return {
		locationTabs, locationTabRef, onGetLocationDataHandler//, onAddLocationHandler
	}
}

//品种信息的选择
export const useBreedTabs=()=>{
	let breedTabs = reactive<ITabsList>({
		list:[ defaultTabItem ],
		choosedTab: defaultTabItem//默认为选择，与list中的
	})
	const breedTabRef = ref<siTabsInstance>();//
	function onGetBreedDataHandler(){
		breedTabs.list.push(
			{ name: '品种1' },
			{ name: '品种2' }
		)
	}
	
	return {
		breedTabs, breedTabRef, onGetBreedDataHandler//, onAddBreedHandler
	}
}